package com.github.edu.web.role.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.github.admin.edu.assembly.common.entity.JsonArray;
import com.github.admin.edu.assembly.common.entity.JsonEntity;
import com.github.admin.edu.assembly.common.util.JsonUtils;
import com.github.admin.edu.assembly.page.entity.Pager;
import com.github.admin.edu.oauth.server.api.entity.TSysRole;
import com.github.admin.edu.oauth.server.api.entity.TSysUser;
import com.github.edu.client.common.service.impl.BaseAbstractService;
import com.github.edu.web.role.domain.RoleManagerDomain;
import com.github.edu.web.role.service.RoleManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * Created by IntelliJ IDEA.
 * user:wangj
 * date:2018/8/12
 * Time: 9:40
 */
@Service
public class RoleManagerServiceImpl extends BaseAbstractService implements RoleManagerService {

    @Autowired
    private RoleManagerDomain domain;

    @Override
    public JsonArray<TSysRole> getAllByPage(TSysRole tSysRole, Pager pager) {
        String code = jsonEntity(tSysRole, pager);
        JsonArray<TSysRole> jsonArray = null;
        if (!StringUtils.isEmpty(code)) {
            String data = domain.getALLByPager(code);
            if (!StringUtils.isEmpty(data)) {
                jsonArray = JsonUtils.toCollection(data, new TypeReference<JsonArray<TSysRole>>() {
                });
            }
        }
        return jsonArray;
    }

    @Override
    public TSysRole getEntity(Long aLong) {
        JsonEntity<TSysRole> jsonEntity = JsonUtils.toCollection(domain.getEntity(aLong), new TypeReference<JsonEntity<TSysRole>>() {
        });
        return jsonEntity.getData();
    }

    @Override
    public TSysRole saveOrUpdate(TSysRole tSysRole) {
        JsonEntity<TSysRole> jsonEntity = new JsonEntity<>();
        jsonEntity.setData(tSysRole);
        String code = domain.saveOrUpdate(JsonUtils.toJson(jsonEntity));
        JsonEntity<TSysRole> json = JsonUtils.toCollection(code, new TypeReference<JsonEntity<TSysRole>>() {
        });
        if (null != json) {
            return json.getData();
        }
        return null;
    }

    @Override
    public String delete(String ids) {
        return domain.deleteAll(ids);
    }
}
